<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Framework\Database\SQL;

use NRG\Framework\Exceptions\QueryException as QueryException;
use NRG\Framework\Exceptions\DatabaseException as DatabaseException;
use NRG\Framework\Exceptions\IllegalArgumentException as IllegalArgumentException;

/**
 * PostgreSQL database
 *
 * @package NRG\Framework\Database\SQL
 */
class Postgresql
{
    /**
     * Stores database connection
     *
     * @var pg_connect
     */
    protected $_db;

    /**
     * Constructor
     *
     * @param string $dbname Name of database
     * @param string $username Username
     * @param string $password Plain text password
     * @param string $host Database host
     * @param string $port Database host port
     * @throws DatabaseException
     */
    public function __construct($dbname, $username, $password, $host="localhost", $port=5432)
    {
        if(trim($dbname) == "")
            throw new DatabaseException(DatabaseException::NO_DBNAME);
        else if(trim($username) == "")
            throw new DatabaseException(DatabaseException::NO_USERNAME);
        else if(trim($password) == "")
            throw new DatabaseException(DatabaseException::NO_PASSWORD);
        else if(trim($host) == "")
            throw new DatabaseException(DatabaseException::NO_HOST);
        else if(!is_integer($port))
            throw new DatabaseException(DatabaseException::PORT_FORMAT);

        // IANA "known", "registered" and "dynamic" port ranges
        if($port < 1 || $port > 65535)
            throw new DatabaseException(DatabaseException::PORT_RANGE);

        if(!$this->_db = pg_connect("host=$host port=$port dbname=$dbname user=$username password=$password"))
            throw new DatabaseException(DatabaseException::CONNECTION_FAILED);
    }

    /**
     * Check for database connection
     *
     * @return boolean
     */
    public function checkConnection()
    {
    return ($this->_db) ? true : false;
    }

    /**
     * Query database with a prepared statement
     *
     * @param string $query Query with parameter placeholders
     * @param array $array Array of query parameters
     * @return array $array Raw pg_execute result
     * @throws DatabaseException
     */
    public function query($query, $array=array())
    {
        if(!is_string($query))
            throw new IllegalArgumentException("Query must be a parameterized string");

        if($query == "")
            throw new IllegalArgumentException("Query cannot be empty");

        $result = array();

        if($this->_db)
        {
            if (empty($array))
                $result = pg_query($this->_db,$query);
            else
                $result = pg_query_params($this->_db, $query, $array);
        }
        else
            throw new DatabaseException(DatabaseException::NO_CONNECTION);

        return $result;
    }

    /**
     * Returns the last postgres error
     * @return string Last error
     */
    public function getLastError()
    {
        return pg_last_notice($this->_db);
    }
}
